package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.Dto.NursingPlanDto;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;


    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    @Transactional
    public NursingPlanVo selectNursingPlanById(Long id) {
/*        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        List<NursingProjectPlanVo> nursingProjectPlan = iNursingProjectPlanService.selectNursingProjectPlanById(id);
        List<NursingProjectPlanVo> collect = nursingProjectPlan.stream().map(nursingProjectPlan1 -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            BeanUtils.copyProperties(nursingProjectPlan1, nursingProjectPlanVo);
            nursingProjectPlanVo.setExecuteCycle(String.valueOf(nursingProjectPlan1.getExecuteCycle()));
            nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan1.getProjectId()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());



        nursingPlanVo.setProjectPlans(collect);

        return nursingPlanVo;*/

        NursingPlan byId = getById(id);

        List<NursingProjectPlan> list = iNursingProjectPlanService.lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, id)
                .list();
        List<NursingProjectPlanVo> collect = list.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            BeanUtils.copyProperties(nursingProjectPlan, nursingProjectPlanVo);
            nursingProjectPlanVo.setExecuteCycle(String.valueOf(nursingProjectPlan.getExecuteCycle()));
            nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());

        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(byId, nursingPlanVo);
        nursingPlanVo.setProjectPlans(collect);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param @OverridN 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan essentialsPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, essentialsPlan);
        save(essentialsPlan);


        int result = 0;
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (ObjectUtils.isNotEmpty(projectPlans)) {
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(essentialsPlan.getId());
            });
            boolean saveResult = iNursingProjectPlanService.saveBatch(projectPlans);
            result = saveResult ? 1 : 0;
        }
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return result;
    }


    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */

    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        if (ObjectUtils.isNotEmpty(projectPlans)) {
            iNursingProjectPlanService.remove(new LambdaQueryWrapper<NursingProjectPlan>()
                    .eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));

            projectPlans.forEach(nursingProjectPlan -> {
                nursingProjectPlan.setPlanId(nursingPlanDto.getId());
            });
            iNursingProjectPlanService.saveBatch(projectPlans);
        }
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);


        Boolean success = updateById(nursingPlan);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return success ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        Boolean success = removeByIds(Arrays.asList(ids));
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return success ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }
}
